package com.neuedu.mblog.service;

import com.neuedu.mblog.dao.DiseaseDao;
import com.neuedu.mblog.entiry.Disease;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：his
 * 创建时间：  2021/12/17   10:22
 * 作者 :jshand
 * 描述 : DiseaseService
 */
public class DiseaseService {

    private DiseaseDao diseaseDao = new DiseaseDao();




    public void queryListByPage(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        Integer pageNum = 1;
        Integer pageSize = 5;

        String strPageNum = request.getParameter("pageNum");
        String strPageSize = request.getParameter("pageSize");
        if(strPageNum !=null && !"".equals(strPageNum)){
            pageNum =  Integer.parseInt(strPageNum);
        }
        if(strPageSize !=null && !"".equals(strPageSize)){
            pageSize =  Integer.parseInt(strPageSize);
        }


        //前端需要的信息
        /**
         * total :
         * pageNum
         * pageSize
         * pageCount
         *
         *  List 表格的内容
         */
        String sql = "SELECT id,  disease_code,  disease_name, disease_icd, cate_id, createtime, valid FROM disease  where  disease_name like  ? ";

        ArrayList paramList = new ArrayList();

        //模糊查询条件
        String diseaseName = request.getParameter("diseaseName");
        if(diseaseName==null){
            diseaseName ="";
        }
        paramList.add("%"+diseaseName+"%");



        long total = diseaseDao.total(sql ,paramList.toArray());
        long pageCount = total%pageSize == 0?total/pageSize:(total/pageSize)+1;

        String pageSql =  sql + " limit  ? ,?";
        paramList.add((pageNum-1) * pageSize);
        paramList.add(pageSize);
        Object[] params = paramList.toArray();

        List<Disease> diseaseList = diseaseDao.selectListByPage(pageSql,params);

        //使用request共享数据
        request.setAttribute("list",diseaseList);
        request.setAttribute("total",total);
        request.setAttribute("pageNum",pageNum);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("pageCount",pageCount);
        System.out.println("pageNum = " + pageNum);
        System.out.println("total = " + total);
        System.out.println("pageSize = " + pageSize);
        System.out.println("pageCount = " + pageCount);
        System.out.println("diseaseList = " + diseaseList);

    }








}
